<<<<<<< HEAD
﻿#include "TimeSync.h"
#include "FrameDiff.h"
#include "UDPCommunication.h"
#include "DecisionFusion.h"
#include <thread>
#include<iostream>
#include <chrono>
#include <iostream>
using namespace std;


int main() {
    // 1. 初始化系统
    const bool isMaster = true;
    TimeSync::init(isMaster);

    // 初始化模块（参数更合理）
    FrameProcessor frameProcessor(640, 480, 16, 25); // 640x480分辨率，16x16块，阈值25
    UDPCommunicator communicator("DEVICE_001", "255.255.255.255"); // 标准广播地址
    DecisionFuser decisionFuser;

    // 2. 启动UDP接收线程
    communicator.startReceiving([&](const EventSummary& event) {
        // 过滤自身发送的事件
        if (event.deviceId != communicator.getDeviceId()) {
            decisionFuser.addPeerEvent(event);
        }
        });

    // 3. 主循环
    while (true) {
        // 时间同步（主节点周期性广播）
        static auto lastSyncTime = std::chrono::steady_clock::now();
        if (isMaster) {
            auto now = std::chrono::steady_clock::now();
            if (std::chrono::duration_cast<std::chrono::seconds>(now - lastSyncTime).count() >= 5) {
                TimeSync::broadcastTimestamp();
                lastSyncTime = now;
            }
        }

        // 图像采集和处理（模拟）
        std::vector<uint8_t> newFrame;
       bool collisionDetected = frameProcessor.processFrame(newFrame);

        if (collisionDetected) {
            // 4. 创建本地事件（置信度基于检测结果动态调整）
            EventSummary localEvent;
            localEvent.deviceId = "DEVICE_001";
            localEvent.timestamp = TimeSync::getSyncedTime();
            localEvent.confidence = 0.75f + (rand() % 10) / 100.0f; // 0.75-0.85随机
            localEvent.eventType = "COLLISION";

            decisionFuser.addLocalEvent(localEvent);

            // 5. 发送事件摘要
            communicator.sendEventSummary(localEvent);

            // 6. 执行决策融合
            EventSummary fusedEvent = decisionFuser.fuseEvents();

            if (fusedEvent.eventType != "NONE") {
                cout << "⚠️ 确认事故! 置信度: " << fusedEvent.confidence
                    << " 来源: " << fusedEvent.deviceId << endl;
                // 实际应用中触发报警逻辑
            }
        }

        // 控制帧率（30fps）
        std::this_thread::sleep_for(std::chrono::milliseconds(33));
    }

    communicator.stopReceiving();
    return 0;
=======
#include "motion.h"
#include "blob.h"
#include "detect.h"
#include <vector>
#include <stdio.h>

uint8_t curr_frame[IMG_W * IMG_H];
uint8_t prev_frame[IMG_W * IMG_H];
uint8_t diff_frame[IMG_W * IMG_H];

std::vector<Blob> blobs;

void get_gray_frame(uint8_t* buf); 

void loop() {
    get_gray_frame(curr_frame);

    compute_frame_diff(curr_frame, prev_frame, diff_frame);

    find_blobs(diff_frame, blobs);
    update_static_frames(blobs);

    if (detect_collision(blobs)) {
        printf("⚠️ 碰撞检测到！\n");
    }

    memcpy(prev_frame, curr_frame, IMG_W * IMG_H);
>>>>>>> 410a8c14512d9ca81fb83f25bcca7a21932aa8de
}